﻿using System.Collections.Generic;
using System.Web.Services;
using BLL;
using BLL.RF.ATETest;
using BLL.RF.PIM;
using BLL.RF.ProductCategory;
using BLL.RF.Repair;
using Model;
using Model.RF.ATETest;
using Model.RF.ProductCategory;

/// <summary>
///PIM_TestAndDataUpload_Service 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。 
// [System.Web.Script.Services.ScriptService]
public class PIM_TestAndDataUpload_Service : System.Web.Services.WebService
{
    private ClientSoft cs;
    private PIMTest pimt;
    private tb_result_DFL tb;
    private ProductType p;
    private ProductType_ZTE pz;
    private ProductType_Flow pf;
    private DebugTest dt;
    private ProductType_MultiBand pm;
    private PIMTest_temp pimt_temp;
    private PIMTestItem pti;
    private Repair repair;
    private RepairRoute rroute;

    public PIM_TestAndDataUpload_Service()
    {
        cs = new ClientSoft();
        pimt = new PIMTest();
        tb = new tb_result_DFL();
        p = new ProductType();
        pz = new ProductType_ZTE();
        pf = new ProductType_Flow();
        dt = new DebugTest();
        pm = new ProductType_MultiBand();
        pimt_temp = new PIMTest_temp();
        pti = new PIMTestItem();
        repair = new Repair();
        rroute = new RepairRoute();
        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod(Description = "判断紫光交调测试客户端软件的版本是否有效，有更新时提供版本号即可")]
    public bool IsValid_PIM_ZiGuang_ClientSoft(string csVersion)
    {
        return !string.IsNullOrEmpty(cs.GetClientSoftbyNV("紫光提供交调软件客户端", csVersion).CSName) && cs.GetClientSoftbyNV("紫光提供交调软件客户端", csVersion).IsValid;
    }

    [WebMethod(Description = "通过SN，从调试记录数据中，获取SN对应的产品类型S代码")]
    public string ProductTypeID_GetbySN_FromDebugTest(string productSN)
    {
        productSN = CommHelper.GetProductSN(productSN);
        if (dt.DebugTest_SelectStepbySN(productSN, "初调").Count > 0)
        {
            return dt.DebugTest_SelectStepbySN(productSN, "初调")[0].ProductTypeID;
        }
        return null;
    }

    [WebMethod(Description = "通过S代码，获取S代码对应的产品类型名称")]
    public string ProductTypeName_GetbyProductTypeID(string productTypeID)
    {
        return p.LoadEntity(productTypeID).ProductTypeName;
    }

    [WebMethod(Description = "交调数据录入时，判断之前工序是否有合格记录")]
    public bool ProductType_Flow_HasValidSevenStep_ForPIMTest(string productSN, string productTypeID)
    {
        return pf.ProductType_Flow_HasValidSevenStep(productSN, productTypeID, "PIMStep");
    }

    [WebMethod(Description = "交调测试时，通过历史数据，判定是否超出允许测试次数，并判定当前测试的‘测试阶段’信息")]
    public string GetTestStep_bySN(string productSN)
    {
        productSN = CommHelper.GetProductSN(productSN);
        int count = pimt.GetPIMTestbyProductSN(productSN).Count;
        switch (count)
        {
            case 0: return "测试";
            default: return "返修测试" + count;
        }
    }

    [WebMethod(Description = "判断产品类型S代码是否合法")]
    public bool IsValidProductTypeID(string productTypeID)
    {
        return !string.IsNullOrEmpty(p.LoadEntity(productTypeID).ProductTypeID);
    }

    [WebMethod(Description = "上传交调测试数据")]
    public bool PIMData_Upload(PIMTestInfo pi, List<PIMTestItemInfo> list)
    {
        bool b = pimt.PIMTestAndItem_Insert(pi, (IList<PIMTestItemInfo>)list);
        //if (b && !pi.IsPass)
        //{
        //    b = Repair_AutoBuild(pi.ProductSN, pi.UserID);
        //}
        return b;
    }

    [WebMethod(Description = "是否需要上传中兴格式数据的产品类型")]
    public bool Isvalid_ProductType_ZTE(string productTypeID)
    {
        return !string.IsNullOrEmpty(pz.LoadEntity(productTypeID).ProductTypeID);
    }

    [WebMethod(Description = "上传中兴格式的交调测试数据")]
    public bool ZTE_PIMTestData_Upload(tb_result_DFL_Info ti, string productTypeID, List<tb_Param_DFL_Info> items)
    {
        ti.IsPIM = true;
        return tb.tb_ATETestAndItem_Insert(ti, productTypeID, items);
    }


    #region 20171113日，增加多频段测试数据合并相关接口

    [WebMethod(Description = "根据产品类型S代码获取需测试的频段信息，返回为空或Count为1时，无需合并")]
    public List<ProductType_MultiBandInfo> GetMultiBandByProductTypeID(string productTypeID)
    {
        List<ProductType_MultiBandInfo> list = (List<ProductType_MultiBandInfo>)pm.ProductType_MultiBand_GetbyPID(productTypeID);
        if (list.Count == 0)
        {
            return null;
        }
        else
        {
            for (int i = list.Count - 1; i >= 0; i--)
            {
                ProductType_MultiBandInfo item = list[i];
                if (item.StepItem != "交调测试")
                    list.Remove(item);
            }
            return list;
        }
    }

    [WebMethod(Description = "根据产品序列号，获取临时数据库中，对应不同频段最新的测试记录")]    
    public List<PIMTest_tempInfo> GetLatestTestbyProductSN(string productSN)
    {
        productSN = CommHelper.GetProductSN(productSN);
        return new List<PIMTest_tempInfo>(pimt_temp.PIMTest_temp_GetLatestbyProductSN(productSN));
    }

    [WebMethod(Description = "多频段产品的测试数据上传至临时数据库，操作成功返回true，否则返回false；tag参数标识是否屏蔽该产品所有历史数据")]
    public bool UploadData_TempDataBase(PIMTest_tempInfo ti, List<PIMTestItemInfo> list, bool tag)
    {
        ti.ProductSN = CommHelper.GetProductSN(ti.ProductSN);
        return tag ? pimt_temp.PIMTest_tempAndItem_Insert(ti, (IList<PIMTestItemInfo>)list) && pimt_temp.PIMTest_temp_DisableRecordsbySN(ti.ProductSN) : pimt_temp.PIMTest_tempAndItem_Insert(ti, (IList<PIMTestItemInfo>)list);
    }

    [WebMethod(Description = "根据测试记录，获取相应测试详细数据，用于获取多份数据后合并")]
    public List<PIMTestItemInfo> GetTestItembyTestInfo(PIMTest_tempInfo ti)
    {
        return new List<PIMTestItemInfo>(pti.GetPIMTestItem_tempbySNDT(ti.ProductSN, ti.TestTime));
    }
    #endregion


    #region 20180125日，增加自动返修相关

    [WebMethod(Description = "测试不合格时，自动生成返修记录，跳转到返修路径")]
    public bool Repair_AutoBuild(string productSN, string userID)
    {
        productSN = CommHelper.GetProductSN(productSN);
        string repairPath = rroute.RepairRoute_GetbyStepItem("PIMStep")[0].RepairPath;
        return repair.Repair_AutoBuild(productSN, repairPath, userID);
    }
    #endregion

    
    #region 20180413日，增加二次交调工序

    [WebMethod(Description = "二次交调，数据录入时，判断之前工序是否有合格记录")]
    public bool ProductType_Flow_HasValidSevenStep_ForPIMSecondTest(string productSN, string productTypeID)
    {
        return pf.ProductType_Flow_HasValidSevenStep(productSN, productTypeID, "PIMSecondStep");
    }

    [WebMethod(Description = "二次交调，测试时，通过历史数据，判定是否超出允许测试次数，并判定当前测试的‘测试阶段’信息")]
    public string GetTestStep_ForPIMSecondTest_bySN(string productSN)
    {
        productSN = CommHelper.GetProductSN(productSN);
        int count = pimt.GetPIMSecondTestbyProductSN(productSN).Count;
        switch (count)
        {
            case 0: return "测试";
            default: return "返修测试" + count;
        }
    }

    [WebMethod(Description = "二次交调，上传测试数据")]
    public bool PIMData_SecondTest_Upload(PIMTestInfo pi, List<PIMTestItemInfo> list)
    {
        bool b = pimt.PIMSecondTestAndItem_Insert(pi, (IList<PIMTestItemInfo>)list);
        return b;
    }
    #endregion
}

